package com.fhz.shop.ui.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.fhz.shop.R;
import com.fhz.shop.entity.GoodsInfoLocal;
import com.fhz.shop.util.ImageLoader;

import java.util.List;

/**
 * 时间:2023/8/18
 *
 * @author Mr.Feng
 * 简述: 首页
 */
public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<HomeRecyclerViewAdapter.ViewHolder> {

    private Context mContext;
    private List<GoodsInfoLocal> mGoodsInfoList;

    //外部点击接口
    public interface OnItemClickListener{
        void onItemClick(View v,int position);
    }
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public HomeRecyclerViewAdapter(Context context, List<GoodsInfoLocal> goodsInfoList) {
        mContext = context;
        mGoodsInfoList = goodsInfoList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private OnItemClickListener mOnItemClickListener;

        private ImageView goodsImage;
        private TextView goodsName;

        public ViewHolder(@NonNull View itemView,OnItemClickListener mOnItemClickListener) {
            super(itemView);
            itemView.setOnClickListener(this);
            this.mOnItemClickListener = mOnItemClickListener;
            goodsImage =  itemView.findViewById(R.id.home_item_goods_image);
            goodsName =  itemView.findViewById(R.id.home_item_goods_name);

        }

        @Override
        public void onClick(View v) {
            mOnItemClickListener.onItemClick(v,getLayoutPosition());
        }
    }

    @NonNull
    @Override
    public HomeRecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //xml -> view
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_item_rv_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(inflate,mOnItemClickListener);

        return viewHolder;
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        GoodsInfoLocal goodsInfoLocal = mGoodsInfoList.get(position);
        holder.goodsName.setText(goodsInfoLocal.getGoodsDesc());
        ImageLoader.load(holder.goodsImage.getContext(),goodsInfoLocal.getGoodsDefaultIcon(),holder.goodsImage);
    }

    @Override
    public int getItemCount() {
        return mGoodsInfoList.size();
    }
}
